package com.yq.cp.service.yw.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.yq.cp.entity.yw.YwBjgl;
import com.yq.cp.entity.yw.YwGdxx;
import com.yq.cp.entity.yw.dto.YwBjglPageDto;
import com.yq.cp.entity.yw.vo.YwBjglVo;
import com.yq.cp.mapper.yw.YwBjglMapper;
import com.yq.cp.service.yw.YwBjglService;
import com.yq.cp.service.yw.YwGdxxService;
import com.yq.cp.utils.DateTimeUtil;
import com.yq.cp.utils.PdfPageUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 运维-报警管理 服务实现类
 * </p>
 *
 * @author zh
 * @since 2024-09-07
 */
@Service
public class YwBjglServiceImpl extends ServiceImpl<YwBjglMapper, YwBjgl> implements YwBjglService {

    @Resource
    YwBjglMapper ywBjglMapper;
    @Resource
    YwGdxxService ywGdxxService;
    @Override
    public IPage<YwBjgl> page(YwBjglVo vo) {
        IPage<YwBjgl> iPage=new Page<>(vo.getPage(),vo.getSize());
        ywBjglMapper.page(iPage,vo);
        return iPage;
    }

    @Override
    public List<YwBjglPageDto> daochu(YwBjglVo vo) {
        vo.setPage(1);
        vo.setSize(1000);
        IPage<YwBjgl> page = page(vo);
        List<YwBjgl> records = page.getRecords();
        List<YwBjglPageDto> list=new ArrayList<>();
        for (int i = 0; i < records.size(); i++) {
            YwBjgl ywBjgl = records.get(i);

            YwBjglPageDto dto=new YwBjglPageDto();
            dto.setId(i+1+"");
            dto.setSszdName(ywBjgl.getSszdName());
            dto.setSblx(ywBjgl.getSblx());
            dto.setBjsb(ywBjgl.getBjsb());
            dto.setBjnr(ywBjgl.getBjnr());
            dto.setCreateTime(DateTimeUtil.getStrDateTime3(ywBjgl.getCreateTime()));
            String bjzt="";
            switch (ywBjgl.getBjzt()){
                case 0:
                  bjzt="未处理";
                  break;
                case 1:
                    bjzt="正在处理";
                    break;
                case 2:
                    bjzt="已恢复";
                    break;
            }
            dto.setBjzt(bjzt);
            list.add(dto);
        }
        return list;
    }

    @Override
    public void sc(Long id) {
        ywBjglMapper.deleteById(id);
    }

    @Override
    public YwBjgl get(Long id) {
        YwBjgl ywBjgl = ywBjglMapper.byId(id);
        return ywBjgl;
    }

    @Override
    public void getDaochu(YwBjgl ywBjgl, HttpServletResponse response) {

        YwGdxx ywGdxx = ywGdxxService.get(ywBjgl.getGdId());

        // 定义全局的字体静态变量
        //定义全局字体静态变量
        Font titlefont;
        Font headfont = null;
        Font headfont1 = null;
        Font keyfont = null;
        Font textfont = null;
        Font textfont1 = null;
        Font content = null;
        Font space = null;
        Font space1 = null;
        Font space2 = null;
        Font space3 = null;
        // 最大宽度
        try {
            // 不同字体（这里定义为同一种字体：包含不同字号、不同style）
            BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            titlefont = new Font(font, 16, Font.BOLD);
            //四号
            headfont = new Font(font, 14, Font.BOLD);
            headfont1 = new Font(font, 14, Font.NORMAL);
            //三号
            content = new Font(font, 16, Font.NORMAL);
            //小四
            textfont = new Font(font, 11, Font.BOLD);
            textfont1 = new Font(font, 11, Font.NORMAL);
            space = new Font(font, 2, Font.NORMAL);
            space1 = new Font(font, 10, Font.NORMAL);
            space2 = new Font(font, 30, Font.NORMAL);
            space3 = new Font(font, 20, Font.NORMAL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        BaseFont bf;
        Font font = null;
        try {
            //创建字体
            bf = BaseFont.createFont( "STSong-Light", "UniGB-UCS2-H",
                    BaseFont.NOT_EMBEDDED);
            //使用字体并给出颜色
            font = new Font(bf,20,Font.BOLD, BaseColor.BLACK);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Document document = new Document(new RectangleReadOnly(842F, 595F));
        //设置页边距  60:左边距，60：右边距，72：上边距，72：下边距
        document.setMargins(60, 60, 72, 72);
        try {
            PdfWriter writer = PdfWriter.getInstance(document, response.getOutputStream());
            PdfPCell cell = null;
            //添加页码
            writer.setPageEvent(new PdfPageUtil());
            //打开生成的pdf文件
            document.open();



            //报警信息
            float[] widthes = {30f};
            PdfPTable tables = new PdfPTable(widthes);

            //未处理故障
            widthes = new float[]{10f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("报警信息",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);


            widthes = new float[]{15f, 25f, 15f, 25f, 15f, 25f, 15f, 25f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("所属站点:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);
            String aaa="aaa";

            cell = new PdfPCell(new Paragraph(ywBjgl.getSszdName(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("设备类型:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(ywBjgl.getSblx(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("设备名称:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(ywBjgl.getBjsb(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("报警状态:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            switch (ywBjgl.getBjzt()){
                case 0:
                    aaa="未处理";
                    break;
                case 1:
                    aaa="正在处理";
                    break;
                case 2:
                    aaa="已恢复";
                    break;
            }

            cell = new PdfPCell(new Paragraph(aaa,textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);




            widthes = new float[]{15f, 25f, 15f, 25f, 15f, 25f, 15f, 25f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("报警内容:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(ywBjgl.getBjnr(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("报警时长:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(ywBjgl.getHoursDiff()+"小时",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("报警时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(DateTimeUtil.getStrDateTime(ywBjgl.getCreateTime()),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph("",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph("",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);



            widthes = new float[]{10f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);

            cell = new PdfPCell(new Paragraph("工单信息",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);



            widthes = new float[]{15f, 25f, 15f, 25f, 15f, 25f, 15f, 25f};
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("工单类型:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(ywGdxx.getGdlxName(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("负责人:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph(ywGdxx.getFzrName(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("运维人员:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(ywGdxx.getZbry(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            cell = new PdfPCell(new Paragraph("最晚处理时间:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setBorder(0);
            cell.setFixedHeight(30);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(DateTimeUtil.getStrDateTime(ywGdxx.getZwslsjTime()),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);



            widthes = new float[]{10f, 90f};
            //PdfPTable tables = new PdfPTable(widthess);
            // tables = new PdfPTable(widthess);
            tables = new PdfPTable(widthes);
            tables.setSpacingBefore(10f);
            tables.setWidthPercentage(100.0f);


            cell = new PdfPCell(new Paragraph("问题描述:",textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);


            cell = new PdfPCell(new Paragraph(ywGdxx.getWtms(),textfont));
            cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
            cell.setHorizontalAlignment(Element.ALIGN_LEFT);
            cell.setFixedHeight(30);
            cell.setBorder(0);
            tables.addCell(cell);

            document.add(tables);

            document.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
